<?php
/**
 * 控制器
 *
 * @author 孙晓晔
 * @version $Id: AdminController.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(APP_LIB . 'Controller.class.php');

class AdminController extends Controller {

	function __construct() {
		parent::__construct();
		$this->application = 'admin';
	}

	/**
	 * 预处理
	 *
	 * @param array 　$request
	 */
	function pretreat($request) {
		$p1 = '';
		session_cache_expire(720);
		if (isset($request['sid'])) {
			session_id($request['sid']);
			session_start();
		} else {
			@session_start();
			$request['sid'] = session_id();
		}

		$p1 .= '&sid=' . $request['sid'];
		$this->assign('sid', $request['sid']);
		// parent::assign('p1', $p1);

		parent::pretreat($request);

		if ($this->isAuth($request)) {
			return true;
		}

		return false;
	}

	/**
	 * 判断是否登录，已登录，则返回false
	 * 若返回true，则不继续执行
	 */
	function isAuth($request) {
		$action = $request['do'];

		if ($this->abc($_SERVER["PHP_SELF"], $action)) {
			return false;
		}
			
		if (empty($_SESSION['name'])) {
			
			$this->assign('postLink', '?do=Admin.loginPost');
			if (isset($request['rnd'])) {
				echo '1';
			} else {
				$this->display($request, 'login');
			}
			
			return true;
		}
			
		if ($_SESSION['name'] == "admin") {
			return false;
		}

		if ($_SESSION['admin'] == "8") {
			return false;
		}
		
		if (preg_match("/(\.admin)$/", $action)) {
			return false;
		}

		require_once(dirname(__FILE__) . '/emp/Admin.class.php');
		$auth = new Admin();
		if (!$auth->isValid($this, $request)) {
			$this->assign('msg', "对不起，操作受限，请联系管理员。");
			$this->display($request, 'info');
			return true;
		}

		return false;
	}

	function abc($cc, $action) {
		if (preg_match("/(.*)(\.ajax)/", $action)) {
			return true;
		}

		if (preg_match("/(.*)(\.audit)/", $action)) {
			return true;
		}

		if (preg_match("/(.*)(\.view)/", $action)) {
			return true;
		}

		if (preg_match("/(.*)(Post)$/", $action, $m)) {
			$action = $m[1];
		}

		preg_match("/([a-z|\\/]+)\\/(.*)/i", $cc, $matches);
		$aa = $matches[2] . "?do=" . $action;

		$public_action = Common::action();
		if (in_array($aa, $public_action)) {
			return true;
		}

		return false;
	}
	
	function execute(&$request) {
	
		parent::execute($request);
	
		$this->_log_action($request);
	}

	function display($request, $resource_name, $cache_id = null, $compile_id = null) {
		parent::display($request, $resource_name, $cache_id, $compile_id);
		
		// $this->_log_action($request);
	}

	function _log_action($request) {
		require_once(dirname(__FILE__) . '/log/LogAction.class.php');
		$aa = new LogAction();
		
		$aa->_insert($request);
	}
}
?>
